﻿var $c = $.extend($c, {});
$c.ajaxFileUpload2 = function (objName, submitUrl, trigger, callback, uploading, options) {
    this.formId = undefined;
    this.targetName = undefined;
    this.form = undefined;
    this.trigger = trigger;
    this.objName = objName;
    this.url = submitUrl;
    this.callback = callback;
    this.uploading = uploading;
    this.options = options;
    this._interval = null;
    this._rnd = undefined;
    this.init = function () {

        //创建于用得交的表单.
        var rnd = parseInt(Math.random() * 1000000000, 10);
        this._rnd = rnd;
        var formId = 'formUpload_' + this._rnd;
        this.formId = formId;
        this.btnSubmitId = 'btnSubmit_' + this._rnd;
        $('body').append("<form id='" + formId + "' name='" + formId + "' method='post' enctype='multipart/form-data'><input type='submit' id='" + this.btnSubmitId + "' style='display:none;'/></form>");

        this.form = $('#' + formId);

        if (this.options && this.options.accept) {
            $(this.form).append("<input type='file' id='file_" + this._rnd + "' name='file_" + this._rnd + "' accept='" + this.options.accept + "' style='display:none;'/>");
        }
        else {
            $(this.form).append("<input type='file' id='file_" + this._rnd + "' name='file_" + this._rnd + "' style='display:none;'/>");
        }

        //创建提交目标iframe.
        var targetName = 'fr_form_' + this._rnd;
        this.targetName = targetName;
        $('body').append("<div style='display:none'><iframe id='" + targetName + "' name='" + targetName + "'></iframe></div>");

        if (this.options) {
            var _form = this.form;
            $.each(this.options, function (name, val) {
                $(_form).append("<input type='hidden' name='" + name + "' value='" + val + "' />");
            });
        }
        this._bindEvent();

        var _this = this;
        $(this.trigger).click(function () {
            $("#" + _this.formId + " input[type='file']").click();
        });
    },

    this.upload = function () {
        var f = document.getElementById(this.formId);
        f.action = this.url + "?objName=" + this.objName;
        f.target = this.targetName;

        var fileName = $(':file', $(this.form)).val();
        var _arr = fileName.split('\\');
        fileName = _arr[_arr.length - 1];

        if (this.uploading && !this.uploading(this, fileName)) {
            this._recreateFileInput();
            return;
        }

        if (fileName.length > 200) {
            this._recreateFileInput();
            alert("文件名太长");
            return;
        }
        this.submitForm(f);

    }
    this.submitForm = function (form, tryCount) {
        if (!tryCount) {
            tryCount = 0;
        }
        if (tryCount > 20) {
            if (confirm('您使用的浏览器无法上传文件,建议使用Firefox浏览器,现在下载Firefox吗？')) {
                window.open('http://www.firefox.com.cn/download/');
            }
            return;
        }
        try {
            form.submit(); //IE、Sogou兼容模式会出异常:SCRIPT5:拒绝访问.
            //$('#' + this.btnSubmitId).click();
        }
        catch (ex) {
            tryCount++;
            this.submitForm(form, tryCount);
        }
    }
    this._recreateFileInput = function () {
        $(':file', this.form).remove();
        if (this.options && this.options.accept) {
            this.form.append("<input type='file' id='file_" + this._rnd + "' name='file_" + this._rnd + "' accept='" + this.options.accept + "' style='display:none;'/>");
        }
        else {
            this.form.append("<input type='file' id='file_" + this._rnd + "' name='file_" + this._rnd + "' style='display:none;'/>");
        }
        this._bindEvent();
    }
    this.finish = function (filepath, filename, code, msg) {
        this._recreateFileInput();
        if (code == 0) {
            if (msg) {
                alert(msg);
            }
            else {
                alert('上传失败');
            }
            return;
        }
        if (this.callback) {
            this.callback(filepath, filename);
        }
    }
    this._bindEvent = function () {
        var _this = this;
        if (this._interval) {
            clearInterval(this._interval); //清除掉,保证永远只有一个Interval
        }
        this._interval = window.setInterval(function () {
            var v = $(':file', $(_this.form)).val();
            if (v && v != '') {
                clearInterval(_this._interval);
                _this._interval = null;
                _this.upload();
            }
        }, 200);
    }
};
